package com.qf.kady.ssm.controller;

import com.qf.kady.ssm.entity.ResultVO;
import com.qf.kady.ssm.mapper.ClassInfoMapper;
import com.qf.kady.ssm.pojo.Book;
import com.qf.kady.ssm.pojo.ClassInfo;
import com.qf.kady.ssm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/4/21
 * \* Time: 11:50
 * \* 千峰JAVA2101班
 * \* Description: 书控制层
 * \
 */
@Controller
@RequestMapping("/admin")
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private ClassInfoMapper classInfoMapper;

    @RequestMapping({"/admin_books.html", "/querybook.html"})
    public ModelAndView showAdminBooks(String searchWord){
        List<Book> resList = bookService.getBookListByCondition(searchWord,null,null);
        ModelAndView mav = new ModelAndView();
        mav.addObject("books",resList);
        mav.addObject("keyword",searchWord!=null?searchWord:"");
        mav.setViewName("admin/admin_books");
        return mav;
    }

    @RequestMapping("/admin_book_detail.html")
    public ModelAndView showAdminBookDetail(Long bookId){
        Book book = bookService.getBookDetailById(bookId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("detail",book);
        mav.setViewName("admin/admin_book_detail");
        return mav;
    }

    @RequestMapping("/book_add.html")
    public ModelAndView toAdminAddBook(Long bookId){
        List<ClassInfo> classInfos = classInfoMapper.selectClassInfoList();
        ModelAndView mav = new ModelAndView();
        mav.addObject("classes",classInfos);
        mav.setViewName("admin/admin_book_add");
        return mav;
    }

    @RequestMapping("/updatebook.html")
    public ModelAndView toAdminUpdateBook(Long bookId){
        Book book = bookService.getBookDetailById(bookId);
        List<ClassInfo> classInfos = classInfoMapper.selectClassInfoList();
        ModelAndView mav = new ModelAndView();
        mav.addObject("detail",book);
        mav.addObject("classes",classInfos);
        mav.setViewName("admin/admin_book_edit");
        return mav;
    }

    @RequestMapping("/book_add_do.html")
    public ModelAndView addBook(Book book, Model model){
        boolean res = bookService.addBook(book);
        if(res){
            model.addAttribute("succ","添加成功");
        }else{
            model.addAttribute("error","添加失败");
        }
        return showAdminBooks(null);
    }

    @RequestMapping("/book_edit_do.html")
    public ModelAndView updateBook(Book book, Model model){
        boolean res = bookService.updateBook(book);
        if(res){
            model.addAttribute("succ","更新成功");
        }else{
            model.addAttribute("error","更新失败");
        }
        return showAdminBooks(null);
    }

    @RequestMapping("/deletebook.html")
    public ModelAndView deleteBook(Long bookId, Model model){
        ResultVO resultVO = bookService.deleteBook(bookId);
        if(resultVO.isSuccess()){
            model.addAttribute("succ",resultVO.getMessage());
        }else{
            model.addAttribute("error",resultVO.getMessage());
        }
        return showAdminBooks(null);
    }
}
